<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class lendplats extends Admin_Controller{
    function __construct() {
        parent::__construct();
        $this->load->model('tree_model');
        $this->load->model('bbs_users_model');
        $this->load->model('bbs_user_group_model');
        $this->load->model('../../../Application/models/reload_rule_model');
        $this->load->model('../../../Application/models/plat_model');
        $this->load->model('../../../Application/models/lend_list_model');
        $this->load->model('admin_log_model');
        $this->load->model('admin_action_model');
        if(!$this->session->userdata('username')){
            redirect('login');
        }
        $username = $this->session->userdata('username');
    }
    function index(){
        //分页
        $where = "1 = 1";
        $st = trim($_GET['st']);
        $key = trim($_GET['key']);
        $type = $_GET['type']?trim($_GET['type']):1;
        if ($key != '') {
            if ($st == 1) {
                $con = '平台中文名称';
                $where .= " and l.name_ch = '$key'";
            }
            if ($st == 2) {
                $con = '平台英文名称';
                $where .= " and l.name_en = '$key'";
            }
        }
        if (trim($_GET['time-start'])) {
            $time_start = strtotime($_GET['time-start']);
            $time_end = strtotime($_GET['time-end']) + 3600 * 24;
            $where .= " and l.create_time >= $time_start";
            
        }
        
        if (trim($_GET['time-end'])) {
            $time_end = strtotime($_GET['time-end']) + 3600 * 24;
            $where .= " and l.create_time <= $time_end";
        }

        $orderby = " is_recommand desc,status desc,sort desc ";
        if ($type==1){
            $where .= " and l.is_mobile = 1";
        }
        if ($type==2){
            $orderby = " id desc ";
            $where .= " and l.is_mobile = 0";
        }

        $getcount = $this->db->query("select l.* from tan_lend_list l where $where order by id desc")->num_rows();
        $navigation_position = 'outside';
        $records_per_page = 10;
        $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position, array('left', 'right')) ? $navigation_position : 'outside');
        //$this->xpage->reverse(true);
        $this->xpage->records($getcount);
        $this->xpage->records_per_page($records_per_page);
        $this->xpage->labels("<<", ">>");
        $query_param = "l.*";
        $list = $this->db->query("select $query_param from tan_lend_list l where $where order by $orderby limit ".(($this->xpage->get_page() - 1) * $records_per_page).",".$records_per_page)->result_array();
        foreach ($list as $key=>$val){
            $rate = ($val['rate'] * 100);
            $list[$key]['rate'] = strlen((explode(".",$rate)[1])) != 3 ? sprintf("%01.2f",$rate)."%" : $rate."%";
        }
        $this->template->assign('stname', $st);
        $this->template->assign('status', $type);
        $this->template->assign('keyname', trim($_GET['key']));
        $this->template->assign('conname', $con);
        $this->template->assign('time_start', trim($_GET['time-start']));
        $this->template->assign('time_end', trim($_GET['time-end']));
        $this->template->assign('Total',$getcount);
        $this->template->assign('showpage',$this->xpage->render(true));
        $this->template->assign('list',$list);
        $this->template->load('lendplats/index');
    }
    
    //用户申请记录
    public function lendlist(){
        //分页
        $where = "1 = 1";
        $st = trim($_GET['st']);
        $key = trim($_GET['key']);
        if ($key != '') {
            if ($st == 1) {
                $con = '平台中文名称';
                $lend = $this->db->query("select id from tan_lend_list where name_ch = '$key' order by id desc limit 1")->row_array();
                $where .= " and l.lend_id = ".($lend ? $lend['id'] : 0);
            }
            if ($st == 2) {
                $con = '平台英文名称';
                $lend = $this->db->query("select id from tan_lend_list where name_en = '$key' order by id desc limit 1")->row_array();
                $where .= " and l.lend_id = ".($lend ? $lend['id'] : 0);
            }
            if($st == 3){
                $con = '用户手机号';
                $users = $this->db->query("select user_id from tan_users where mobile = '$key' order by user_id desc limit 1")->row_array();
                $where .= " and l.user_id = ".($users ? $users['user_id'] : 0);
            }
        }
        if (trim($_GET['time-start'])) {
            $time_start = strtotime($_GET['time-start']);
            $time_end = strtotime($_GET['time-end']) + 3600 * 24;
            $where .= " and l.apply_time >= $time_start";

        }
        
        if (trim($_GET['time-end'])) {
            $time_end = strtotime($_GET['time-end']) + 3600 * 24;
            $where .= " and l.apply_time <= $time_end";

        }
        
        $getcount = $this->db->query("select l.* from tan_users_lend_list l left join tan_lend_list ll on l.lend_id = ll.id where $where order by id desc")->num_rows();
        $navigation_position = 'outside';
        $records_per_page = 10;
        $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position, array('left', 'right')) ? $navigation_position : 'outside');
        //$this->xpage->reverse(true);
        $this->xpage->records($getcount);
        $this->xpage->records_per_page($records_per_page);
        $this->xpage->labels("<<", ">>");
        $query_param = "l.*,ll.name_en,ll.name_ch,u.mobile";
        $list = $this->db->query("select $query_param from tan_users_lend_list l left join tan_lend_list ll on l.lend_id = ll.id left join tan_users u on u.user_id = l.user_id where $where order by l.id desc limit ".(($this->xpage->get_page() - 1) * $records_per_page).",".$records_per_page)->result_array();
        foreach ($list as $key=>$val){
            $list[$key]['apply_time'] = date("Y-m-d H:i",$val['apply_time']);
        }
        $this->template->assign('stname', $st);
        $this->template->assign('keyname', trim($_GET['key']));
        $this->template->assign('conname', $con);
        $this->template->assign('time_start', trim($_GET['time-start']));
        $this->template->assign('time_end', trim($_GET['time-end']));
        $this->template->assign('Total',$getcount);
        $this->template->assign('showpage',$this->xpage->render(true));
        $this->template->assign('list',$list);
         $this->template->load('lendplats/lendlist');
    }
    //信用卡类别
    public function platcate(){
        $where = array();
        $where['pid'] = 0;
        $list = $this->db->select('*')->from("tan_lend_category")->where($where)->order_by("sort desc,id desc")->limit(10)->get()->result_array(); 
        $this->template->assign('list',$list);
        $this->template->load('lendplats/platcate');
    }
    //添加信用卡分类
    public function category_add(){
        if($_POST){
            $data = array();
            $data['title'] = trim($_POST['title']);
            $data['code'] = trim($_POST['code']);
            $data['sort'] = trim($_POST['sort']);
            $data['desc'] = trim($_POST['desc']);
            $data['is_hide'] = trim($_POST['is_hide']);
            $data['groups'] = $data['code'];
            $data['pid'] = 0;
            $data['level'] = 0;
            if(!$data['title']){
                $mssage['info']	  = '类别名称不能为空';
                $mssage['status'] = 0;
                $mssage['url']	  = "";
                echo json_encode($mssage);exit;
            }
            
            if(!$data['code']){
                $mssage['info']	  = '类别标识不能为空';
                $mssage['status'] = 0;
                $mssage['url']	  = "";
                echo json_encode($mssage);exit;
            }
            
            $temp = $this->db->query("select * from tan_lend_category where code = '$data[code]' order by id desc")->num_rows();
            if($temp){
                $mssage['info']	  = '类别标识不能重复';
                $mssage['status'] = 0;
                $mssage['url']	  = "";
                echo json_encode($mssage);exit;
            }
            
            $result = $this->db->insert("tan_lend_category",$data);
            if($result){
                $mssage['info']	  = '添加成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url('lendplats/platcate');
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '添加失败';
                $mssage['status'] = 0;
                $mssage['url']	  = site_url('lendplats/platcate');
                echo json_encode($mssage);exit;
            }
            
        }
        $this->template->assign('segment_p','platcate');
        $this->template->load('lendplats/category_add');
    }
    //修改信用卡分类
    public function category_edit(){
        if($_POST){
            $id = trim($_POST['id']);
            $data = array();
            $data['title'] = trim($_POST['title']);
            $data['desc'] = trim($_POST['desc']);
            $data['sort'] = trim($_POST['sort']);
            $data['is_hide'] = trim($_POST['is_hide']);
            if(!$data['title']){
                $mssage['info']	  = '类别名称不能为空';
                $mssage['status'] = 0;
                $mssage['url']	  = "";
                echo json_encode($mssage);exit;
            }
            
            $result = $this->db->where(array("id"=>$id))->update("tan_lend_category",$data);
            if($result){
                $mssage['info']	  = '修改成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url('lendplats/platcate');
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '修改失败';
                $mssage['status'] = 0;
                $mssage['url']	  = site_url('lendplats/platcate');
                echo json_encode($mssage);exit;
            }
            
        }
        $id = isset($_GET['id']) ? trim($_GET['id']) : 0;
        $item = $this->db->query("select * from tan_lend_category where id = $id order by id desc limit 1")->row_array();
        if(!$item){redirect("lendplats/category");}
        
        $this->template->assign('item',$item);
        $this->template->assign('segment_p','platcate');
        $this->template->load('lendplats/category_edit');
    }
    //子分类列表
    function category_son(){
        $pid = trim($_GET['pid']);
        if(!$pid){
            redirect("lendplats/platcate");
        }
        $where = array();
        $where['pid'] = $pid;
        $getcount = $this->db->query("select * from tan_lend_category where pid = $pid order by id desc")->num_rows();
        $navigation_position = 'outside';
        $records_per_page = 10;
        $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position, array('left', 'right')) ? $navigation_position : 'outside');
        //$this->xpage->reverse(true);
        $this->xpage->records($getcount);
        $this->xpage->records_per_page($records_per_page);
        $this->xpage->labels("<<", ">>");
        $list = $this->db->query("select * from tan_lend_category where pid = $pid order by sort desc limit ".(($this->xpage->get_page() - 1) * $records_per_page).",".$records_per_page)->result_array();
        
        $item = $this->db->query("select * from tan_lend_category where id = $pid order by id desc limit 1")->row_array();
        $this->template->assign('item',$item);
        $this->template->assign('list',$list);
        $this->template->assign('Total',$getcount);
        $this->template->assign('showpage',$this->xpage->render(true));
        $this->template->assign('segment_p','platcate');
        $this->template->load('lendplats/category_son');
    }
    //添加子分类
    function category_son_add(){
        if($_POST){
            $data = array();
            $data['title'] = trim($_POST['title']);
            $data['code'] = "";
            $data['desc'] = trim($_POST['desc']);
            $data['groups'] = trim($_POST['groups']);
            $data['pid'] = trim($_POST['pid']);
            $data['is_hide'] = trim($_POST['is_hide']);
            $data['sort'] = trim($_POST['sort']);
            $data['level'] = trim($_POST['level']) + 1;
            if(!$data['title']){
                $mssage['info']	  = '类别名称不能为空';
                $mssage['status'] = 0;
                $mssage['url']	  = "";
                echo json_encode($mssage);exit;
            }
            
            $result = $this->db->insert("tan_lend_category",$data);
            if($result){
                
                $mssage['info']	  = '添加成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url('lendplats/category_son')."?pid=".$data['pid'];
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '添加失败';
                $mssage['status'] = 0;
                $mssage['url']	  = site_url('lendplats/category_son')."?pid=".$data['pid'];
                echo json_encode($mssage);exit;
            }
            
        }
        $pid = isset($_GET['pid']) ? trim($_GET['pid']) : 0;
        $item = $this->db->query("select * from tan_lend_category where id = $pid order by id desc limit 1")->row_array();
        if(!$item){
            redirect("lendplats/platcate");
        }
        $this->template->assign('item',$item);
        $this->template->assign('segment_p','platcate');
        $this->template->load('lendplats/category_son_add');
    }
    //修改子分类信息
    function category_son_edit(){
        if($_POST){
            $data = array();
            $id = trim($_POST['id']);
            $pid = trim($_POST["pid"]);
            $data['desc'] = trim($_POST['desc']);
            $data['title'] = trim($_POST['title']);
            $data['is_hide'] = trim($_POST['is_hide']);
            $data['sort'] = trim($_POST['sort']);
            if(!$data['title']){
                $mssage['info']	  = '类别名称不能为空';
                $mssage['status'] = 0;
                $mssage['url']	  = "";
                echo json_encode($mssage);exit;
            }
            
            $result = $this->db->where(array("id"=>$id))->update("tan_lend_category",$data);
            if($result){
                update_cache("credit");
                $mssage['info']	  = '添加成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url('lendplats/category_son')."?pid=".$pid;
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '添加失败';
                $mssage['status'] = 0;
                $mssage['url']	  = site_url('lendplats/category_son')."?pid=".$pid;
                echo json_encode($mssage);exit;
            }
            
        }
        $id = isset($_GET['id']) ? trim($_GET['id']) : 0;
        $item = $this->db->query("select * from tan_lend_category where id = $id order by id desc limit 1")->row_array();
        if(!$item){
            $url = site_url("credit/category_son")."?pid=".$_GET['pid'];
            redirect($url);
        }
        $this->template->assign('item',$item);
        $this->template->assign('segment_p','platcate');
        $this->template->load('lendplats/category_son_edit');
    }
    
    
    //删除子分类
    function category_son_del(){
        if($_GET){
            $mssage = array();
            $category_id	   = trim($_GET['id']);
            $data = array();
            $result = $this->db->where(array("id"=>$category_id))->delete("tan_lend_category");
            if($result){
                $mssage['info']	  = '删除成功';
                $mssage['status'] = 1;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '操作失败';
                $mssage['status'] = 0;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }

        }
    }
    
    function setcategory_hide(){
        if($_GET){
            $mssage = array();
            $category_id	   = trim($_GET['id']);
            $is_hide = trim($_GET['is_hide']);
            $data = array();
            $data['is_hide'] = $is_hide;
            $result = $this->db->where(array("id"=>$category_id))->update("tan_lend_category",$data);
            if($result){
                if($is_hide==1){
                    $mssage['info']	  = '显示成功';
                }else{
                    $mssage['info']	  = '隐藏成功';
                }
                $mssage['status'] = 1;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '操作失败';
                $mssage['status'] = 0;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }

        }
    }
    
    
    public function setstatus()
    {	
        if($_GET){
            $mssage = array();
            $id	   = trim($_GET['id']);
            $status = trim($_GET['status']);
            $type = trim($_GET['type']);
            if($type==""){
            $result = $this->bbs_article_model->setstatus($status,$id);
            }else{
            $result = $this->bbs_article_model->status($status,$id,$type);    
            }
            if($result){
                 $mssage['info']	  = '操作成功';
                 $mssage['status'] = 1;
                 $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                 echo json_encode($mssage);exit;
            }else{
                 $mssage['info']	  = '操作失败';
                 $mssage['status'] = 0;
                 $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                 echo json_encode($mssage);exit;
            }
        }	
    }
    
    
    //贷款平台添加
    public function add(){
        if($_POST){

            $data = array();
            $data['name_ch'] = trim($_POST['name_ch']);
            $data['name_en'] = trim($_POST['name_en']);
            $data['lend_id'] = $_POST['lend_id']?implode(',',$_POST['lend_id']):"";
            $data['amount_id'] = trim($_POST['amount_id']);
            $data['rate_id'] = trim($_POST['rate_id']);
            $data['period_id'] = trim($_POST['period_id']);
            $data['rate'] = trim($_POST['rate']);
            $data['period_area'] = trim($_POST['period_area']);
//            $data['amount_area'] = trim($_POST['amount_area']);
//            $data['amount_area_alisa'] = trim($_POST['amount_area_alisa']);
            $data['amount_first'] = intval($_POST['amount_first']);
            $data['amount_over'] = intval($_POST['amount_over']);
            $data['period_area_arr'] = trim($_POST['period_area_arr']);
            $data['amount_area_arr'] = trim($_POST['amount_area_arr']);
            $data['tip_list'] = trim($_POST['tip_list']);
            $data['tip_detail'] = trim($_POST['tip_detail']);
            $data['tip_lending_fast'] = trim($_POST['tip_lending_fast']);
            $data['sort'] = trim($_POST['sort']);
            $data['logo_list'] = trim($_POST['logo_list']);
            $data['logo_detail'] = trim($_POST['logo_detail']);
            $data['logo_icon'] = trim($_POST['logo_icon']);
            $data['logo_small'] = trim($_POST['logo_small']);
            $data['description'] = trim($_POST['description']);
            $data['apply_conditions'] = trim($_POST['apply_conditions']);
            $data['apply_materials'] = trim($_POST['apply_materials']);
            $data['h5_reg_url'] = trim($_POST['h5_reg_url']);

            $data['plat_reg_url'] = json_encode($_POST['plat_url']);

            $data['contact_number'] = trim($_POST['contact_number']);
            $data['create_time'] = time();
            $data['status'] = 1;

            $result = $this->db->insert("tan_lend_list",$data);
            if($result){

                $logdata = array();
                $logdata['user_id'] = $this->userinfo['id'];
                $logdata['username'] = $this->userinfo['username'];
                $logdata['action_cate'] = 1;
                $logdata['event'] = 'lendplat';
                $logdata['event_id'] = $this->db->insert_id();
                $logdata['action'] = 'add';
                $logdata['action_val'] = 0;
                $logdata['content'] = '新增平台：'.$data['name_ch'];
                $this->admin_action_model->insert($logdata);

                $mssage['info']	  = '添加成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url("lendplats/index");
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '添加失败';
                $mssage['status'] = 0;
                $mssage['url']	  = site_url("lendplats/index");
                echo json_encode($mssage);exit;
            }
        }
        $plat_list = $this->db->query("SELECT * FROM tan_dictionary WHERE type=1")->result_array();
        $this->template->assign('plat_list', $plat_list);

        $amount_type = $this->db->query("select * from tan_lend_category where groups = 'amount_type' order by id asc")->result_array();
        $rate_type = $this->db->query("select * from tan_lend_category where groups = 'rate_type' order by id asc")->result_array();
        $period_type = $this->db->query("select * from tan_lend_category where groups = 'period_type' order by id asc")->result_array();
        $lend_type = $this->db->query("select * from tan_lend_category where groups = 'lend_type' and pid>0 order by id asc")->result_array();
        $this->template->assign('amount_type',$amount_type);
        $this->template->assign('rate_type',$rate_type);
        $this->template->assign('period_type',$period_type);
        $this->template->assign('lend_type',$lend_type);
        
        $this->template->assign('segment_p','index');
        $this->template->load('lendplats/add');
    }
    
    //贷款平台修改
    public function edit(){
        if($_POST){

            $id = trim($_POST['id']);
            $lend_info = $this->db->query("select * from tan_lend_list where id = $id order by id desc limit 1")->row_array();
            if ($lend_info['disable_edit']==1){
                $mssage['info']	  = '当前平台禁止修改';
                $mssage['status'] = 0;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }

            $data = array();
            $data['name_ch'] = trim($_POST['name_ch']);
            $data['name_en'] = trim($_POST['name_en']);
            $data['lend_id'] = $_POST['lend_id']?implode(',',$_POST['lend_id']):'';
            $data['amount_id'] = trim($_POST['amount_id']);
            $data['rate_id'] = trim($_POST['rate_id']);
            $data['period_id'] = trim($_POST['period_id']);
            $data['rate'] = trim($_POST['rate']);
            $data['period_area'] = trim($_POST['period_area']);
//            $data['amount_area'] = trim($_POST['amount_area']);
//            $data['amount_area_alisa'] = trim($_POST['amount_area_alisa']);
            $data['amount_first'] = intval($_POST['amount_first']);
            $data['amount_over'] = intval($_POST['amount_over']);
            $data['period_area_arr'] = trim($_POST['period_area_arr']);
            $data['amount_area_arr'] = trim($_POST['amount_area_arr']);
            $data['tip_list'] = trim($_POST['tip_list']);
            $data['tip_detail'] = trim($_POST['tip_detail']);
            $data['tip_lending_fast'] = trim($_POST['tip_lending_fast']);
            $data['sort'] = trim($_POST['sort']);
            $data['logo_list'] = trim($_POST['logo_list']);
            $data['logo_detail'] = trim($_POST['logo_detail']);
            $data['logo_icon'] = trim($_POST['logo_icon']);
            $data['logo_small'] = trim($_POST['logo_small']);
            $data['description'] = trim($_POST['description']);
            $data['apply_conditions'] = trim($_POST['apply_conditions']);
            $data['apply_materials'] = trim($_POST['apply_materials']);
            $data['h5_reg_url'] = trim($_POST['h5_reg_url']);
            $data['plat_reg_url'] = json_encode($_POST['plat_url']);
            $data['contact_number'] = trim($_POST['contact_number']);
            $data['status'] = 1;
            $data['create_time'] = time();
            if($data['status'] != 1){
                    $data['is_recommand'] = 0;
                     $data['is_mobile'] = 0;
            }
            $result = $this->db->where(array("id"=>$id))->update("tan_lend_list",$data);
            if($result){

                $logdata = array();
                $logdata['user_id'] = $this->userinfo['id'];
                $logdata['username'] = $this->userinfo['username'];
                $logdata['action_cate'] = 1;
                $logdata['event'] = 'lendplat';
                $logdata['event_id'] = $id;
                $logdata['action'] = 'edit';
                $logdata['action_val'] = 0;
                $logdata['content'] = '编辑平台：'.$data['name_ch'];
                $this->admin_action_model->insert($logdata);

                $mssage['info']	  = '修改成功';
                $mssage['status'] = 1;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '修改失败';
                $mssage['status'] = 0;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }
        }

        $amount_type = $this->db->query("select * from tan_lend_category where groups = 'amount_type' order by id asc")->result_array();
        $rate_type = $this->db->query("select * from tan_lend_category where groups = 'rate_type' order by id asc")->result_array();
        $period_type = $this->db->query("select * from tan_lend_category where groups = 'period_type' order by id asc")->result_array();
        $lend_type = $this->db->query("select * from tan_lend_category where groups = 'lend_type' and pid>0 order by id asc")->result_array();

        $plat_list = $this->db->query("SELECT * FROM tan_dictionary WHERE type=1")->result_array();
        $this->template->assign('plat_list', $plat_list);

        $this->template->assign('amount_type',$amount_type);
        $this->template->assign('rate_type',$rate_type);
        $this->template->assign('period_type',$period_type);
        $this->template->assign('lend_type',$lend_type);

        $id = trim($_REQUEST['id']);
        $item = $this->db->query("select * from tan_lend_list where id = $id order by id desc limit 1")->row_array();
        $item['lend_id'] = explode(',',$item['lend_id']);
        $item['plat_reg_url'] = json_decode($item['plat_reg_url'],true);

        $this->template->assign('item',$item);
        $this->template->assign('segment_p','index');
        $this->template->load('lendplats/edit');
    }
    function plataction(){
        $where = ' action_cate = 1 ';
        if (trim($_GET['time-start'])) {
            $time_start = strtotime($_GET['time-start']);
            $where .= " AND add_time >= $time_start";
        }

        if (trim($_GET['time-end'])) {
            $time_end = strtotime($_GET['time-end']) + 3600 * 24;
            $where .= " AND add_time <= $time_end";
        }

        $getcount = $this->admin_action_model->get_count($where);

        $navigation_position = 'outside';
        $records_per_page = 20;
        $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position,
            array('left', 'right')) ? $navigation_position : 'outside');
        //$this->xpage->reverse(true);
        $this->xpage->records($getcount);
        $this->xpage->records_per_page($records_per_page);
        $this->xpage->labels("<<", ">>");

        $list = $this->admin_action_model->get_list($where,(($this->xpage->get_page() - 1) * $records_per_page),$records_per_page);

        foreach ($list as $item=>$value){
            $list[$item]['add_time'] = date('Y-m-d H:i:s',$value['add_time']);
            switch ($value['action']){
                case 'add':
                    $list[$item]['action'] = "新增";
                    break;
                case 'edit':
                    $list[$item]['action'] = "修改";
                    break;
                case 'is_mobile':
                    $list[$item]['action'] = "手机显示";
                    break;
                case 'is_recommand':
                    $list[$item]['action'] = "是否推荐";
                    break;

            }

            if ($value['action_val']==0){
                $list[$item]['action_val'] = "关闭";
            }
            if ($value['action_val']==1){
                $list[$item]['action_val'] = "开启";
            }
            $plats_info = $this->db->query("SELECT name_ch FROM tan_lend_list WHERE id=".$value['event_id'])->row_array();
            $list[$item]['event_name'] = $plats_info['name_ch'];
        }

        $this->template->assign('time_start', $_GET['time-start']);
        $this->template->assign('time_end', $_GET['time-end']);
        $this->template->assign('Total', $getcount);
        $this->template->assign('showpage', $this->xpage->render(true));
        $this->template->assign('list', $list);
        $this->template->assign('segment_p','plataction');
        $this->template->load('lendplats/plataction');
    }
    function plattrends(){
        $where = ' action_cate = 1 ';
        if (trim($_GET['time'])) {
            $time_start = strtotime($_GET['time']);
            $time_end = strtotime($_GET['time']) + 3600 * 24;
        }else{
            $_GET['time'] = date('Y-m-d',time());
            $time_start = strtotime(date('Y-m-d',time()));
            $time_end = strtotime(date('Y-m-d',time())) + 3600 * 24;
        }

        $where .= " AND add_time >= $time_start AND add_time <= $time_end";

        $getcount = $this->admin_action_model->get_Acount($where);
//        $navigation_position = 'outside';
//        $records_per_page = 20;
//        $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position,
//            array('left', 'right')) ? $navigation_position : 'outside');
//        //$this->xpage->reverse(true);
//        $this->xpage->records($getcount);
//        $this->xpage->records_per_page($records_per_page);
//        $this->xpage->labels("<<", ">>");
        $lend_idArr =  $this->admin_action_model->get_Alist($where);
        $lend_idStr = "";
        foreach ($lend_idArr as $idkey=>$idval){
            $lend_idStr .= $idval['event_id'].',';
        }
        $lend_list=array();
        if (trim($lend_idStr,',')){
            $lend_list = $this->db->query("SELECT id,name_ch,create_time FROM tan_lend_list WHERE id IN(".trim($lend_idStr,',').")")->result_array();

            foreach ($lend_list as $lendkey=>$lendval){
                if ($lendval['create_time']>$time_start && $lendval['create_time']<$time_end){
                    $lend_list[$lendkey]['is_new'] = 1;
                }
                $Awhere = $where . " AND action='is_mobile' AND action_val=1 AND event_id = ".$lendval['id'];
                $lend_list[$lendkey]['is_one'] = $this->admin_action_model->get_count($Awhere);
                $Bwhere = $where . " AND action='is_mobile' AND action_val=0 AND event_id = ".$lendval['id'];
                $lend_list[$lendkey]['is_zore'] = $this->admin_action_model->get_count($Bwhere);

                $Cwhere = $where . " AND action='is_recommand' AND action_val=1 AND event_id = ".$lendval['id'];
                $lend_list[$lendkey]['is_rec'] = $this->admin_action_model->get_count($Cwhere);
                $Dwhere = $where . " AND action='is_recommand' AND action_val=0 AND event_id = ".$lendval['id'];
                $lend_list[$lendkey]['not_rec'] = $this->admin_action_model->get_count($Dwhere);
            }
        }
        if ($_GET['time'] == date('Y-m-d',time())){
            $today_lend_idArr = $this->db->query("select id from tan_lend_list WHERE status = 1 and is_mobile=1 order by id desc")->result_array();
        }else{
            $today_lend_idStr = $this->db->query("SELECT lend_id FROM tan_lend_day_log WHERE time_date ='".$_GET['time']."' limit 1")->row_array();
            $today_lend_idArr = $today_lend_idStr['lend_id']?explode(',',$today_lend_idStr['lend_id']):array();
        }

        $this->template->assign('today_lend_num', count($today_lend_idArr));
        $this->template->assign('left_time', $_GET['time']);
        $this->template->assign('Total', $getcount);
//        $this->template->assign('showpage', $this->xpage->render(true));
        $this->template->assign('list', $lend_list);
        $this->template->assign('segment_p','plattrends');
        $this->template->load('lendplats/plattrends');

    }
    function plattrends_list(){
        $where = ' action_cate = 1 ';

        if (trim($_GET['lend_id'])) {
            $where .= " AND event_id = ".intval(trim($_GET['lend_id']));
        }
        switch (intval($_GET['type'])){
            case 1:
                $where .= " AND action='is_mobile' AND action_val = 1";
                break;
            case 2:
                $where .= " AND action='is_mobile' AND action_val = 0";
                break;
            case 3:
                $where .= " AND action='is_recommand' AND action_val = 1";
                break;
            case 4:
                $where .= " AND action='is_recommand' AND action_val = 0";
                break;
        }

        if (trim($_GET['time'])) {
            $time_start = strtotime($_GET['time']);
            $time_end = strtotime($_GET['time']) + 3600 * 24;
        }else{
            $time_start = date('Y-m-d',time());
            $time_end = strtotime(date('Y-m-d',time())) + 3600 * 24;
        }
        $where .= " AND add_time >= $time_start AND add_time <= $time_end";

        $getcount = $this->admin_action_model->get_count($where);
        $navigation_position = 'outside';
        $records_per_page = 20;
        $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position,
            array('left', 'right')) ? $navigation_position : 'outside');
        //$this->xpage->reverse(true);
        $this->xpage->records($getcount);
        $this->xpage->records_per_page($records_per_page);
        $this->xpage->labels("<<", ">>");
        $list =  $this->admin_action_model->get_list($where,(($this->xpage->get_page() - 1) * $records_per_page),$records_per_page);
        foreach ($list as $item=>$value){
            $list[$item]['add_time'] = date('Y-m-d H:i:s',$value['add_time']);
            switch ($value['action']){
                case 'add':
                    $list[$item]['action'] = "新增";
                    break;
                case 'edit':
                    $list[$item]['action'] = "修改";
                    break;
                case 'is_mobile':
                    $list[$item]['action'] = "手机显示";
                    break;
                case 'is_recommand':
                    $list[$item]['action'] = "是否推荐";
                    break;

            }

            if ($value['action_val']==0){
                $list[$item]['action_val'] = "关闭";
            }
            if ($value['action_val']==1){
                $list[$item]['action_val'] = "开启";
            }
            $plats_info = $this->db->query("SELECT name_ch FROM tan_lend_list WHERE id=".$value['event_id'])->row_array();
            $list[$item]['event_name'] = $plats_info['name_ch'];
        }
        $this->template->assign('time', $_GET['time']);
        $this->template->assign('Total', $getcount);
        $this->template->assign('showpage', $this->xpage->render(true));
        $this->template->assign('list', $list);
        $this->template->assign('segment_p','plattrends');
        $this->template->load('lendplats/plataction');

    }
    //帖子置顶顺序
    function lend_setsort(){
        if($_POST){
            $mssage = array();

            $id    = trim($_POST['id']);
            $sort  = trim($_POST['sort']);
            if(!$id){
                    $mssage['info']	  = '系统错误';
                    $mssage['status'] = 0;
                    $mssage['url']	  = '';
                    echo json_encode($mssage);exit;
            }

            $data = array();
            $data['sort'] = $sort;
            $result = $this->db->where(array("id"=>$id))->update("tan_lend_list",$data);
            if($result){
                $mssage['info']	  = '修改成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url('lendplats/index');
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '修改失败';
                $mssage['status'] = 0;
                $mssage['url']	  = site_url('lendplats/index');
                echo json_encode($mssage);exit;
            }
        }
    }
    //帖子置顶顺序
    function lend_setinfo(){
        if($_REQUEST){
            $mssage = array();

            $id    = trim($_REQUEST['id']);
            $key  = trim($_REQUEST['key']);
            $value  = trim($_REQUEST['value']);
            if(!$id){
                    $mssage['info']	  = '系统错误';
                    $mssage['status'] = 0;
                    $mssage['url']	  = '';
                    echo json_encode($mssage);exit;
            }

            $data = array();
            $data[$key] = $value;
            if ($key=='is_mobile' && $value==1){
                $data['disable_edit'] = 1;
            }
            $result = $this->db->where(array("id"=>$id))->update("tan_lend_list",$data);
            if($result){
                $logdata = array();
                $logdata['user_id'] = $this->userinfo['id'];
                $logdata['username'] = $this->userinfo['username'];
                $logdata['action_cate'] = 1;
                $logdata['event'] = 'lendplat';
                $logdata['event_id'] = $id;
                $logdata['action'] = $key;
                $logdata['action_val'] = $value;
                $logdata['content'] = '平台操作：'.$key.'='.$value;
                $this->admin_action_model->insert($logdata);

                $mssage['info']	  = '修改成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url('lendplats/index');
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '修改失败';
                $mssage['status'] = 0;
                $mssage['url']	  = site_url('lendplats/index');
                echo json_encode($mssage);exit;
            }
        }
    }
    //帖子置顶顺序
    function lend_setcountsuccess(){
        if($_POST){
            $mssage = array();

            $id    = trim($_POST['id']);
            $count_success  = trim($_POST['count_success']);
            if(!$id){
                    $mssage['info']	  = '系统错误';
                    $mssage['status'] = 0;
                    $mssage['url']	  = '';
                    echo json_encode($mssage);exit;
            }

            $data = array();
            $data['count_success'] = $count_success;
            $result = $this->db->where(array("id"=>$id))->update("tan_lend_list",$data);
            if($result){
                $mssage['info']	  = '修改成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url('lendplats/index');
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '修改失败';
                $mssage['status'] = 0;
                $mssage['url']	  = site_url('lendplats/index');
                echo json_encode($mssage);exit;
            }
        }
    }
        
    //用户贷款评论记录
    function commentlist(){
        //分页
        $where = "1 = 1";
        $st = trim($_GET['st']);
        $key = trim($_GET['key']);
        if ($key != '') {
            $st = $st ? $st : 1;
            if ($st == 1) {
                $lend = $this->db->query("select id from tan_lend_list where name_ch = '$key' order by id desc limit 1")->row_array();
                $con = '平台中文名称';
                $where .= " and c.lend_id = $lend[id]";
            }
            if ($st == 2) {
                $lend = $this->db->query("select id from tan_lend_list where name_en = '$key' order by id desc limit 1")->row_array();
                $con = '平台英文名称';
                $where .= " and c.lend_id = $lend[id]";
            }
            if ($st == 3) {
                $user = $this->db->query("select user_id from tan_users where mobile = '$key' order by user_id desc limit 1")->row_array();
                $con = '平台英文名称';
                $where .= " and c.user_id = $user[user_id]";
            }
        }
        if (trim($_GET['time-start'])) {
            $time_start = strtotime($_GET['time-start']);
            $where .= " and c.create_time >= $time_start";
        }
        
        if (trim($_GET['time-end'])) {
            $time_end = strtotime($_GET['time-end']) + 3600 * 24;
            $where .= "and c.create_time <= $time_end";
            
        }
        
        $getcount = $this->db->query("select c.* from tan_lend_comment c left join tan_lend_list l on l.id = c.lend_id where $where order by c.id desc")->num_rows();
        $navigation_position = 'outside';
        $records_per_page = 10;
        $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position, array('left', 'right')) ? $navigation_position : 'outside');
        //$this->xpage->reverse(true);
        $this->xpage->records($getcount);
        $this->xpage->records_per_page($records_per_page);
        $this->xpage->labels("<<", ">>");
        $query_param = "c.*,l.name_ch,l.name_en";
        $list = $this->db->query("select $query_param from tan_lend_comment c left join tan_lend_list l on l.id = c.lend_id where $where order by c.id desc limit ".(($this->xpage->get_page() - 1) * $records_per_page).",".$records_per_page)->result_array();
        foreach ($list as $key=>$val){
            if(!$val['is_line']){
                $user = $this->db->query("select mobile from tan_users where user_id = $val[user_id] order by user_id desc limit 1")->row_array();
                $list[$key]['mobile'] = $user['mobile'];
            }
            $list[$key]['create_time'] = date("Y-m-d H:i",$val['create_time']);
        }
        $this->template->assign('stname', $st);
        $this->template->assign('keyname', trim($_GET['key']));
        $this->template->assign('conname', $con);
        $this->template->assign('time_start', trim($_GET['time-start']));
        $this->template->assign('time_end', trim($_GET['time-end']));
        $this->template->assign('Total',$getcount);
        $this->template->assign('showpage',$this->xpage->render(true));
        $this->template->assign('list',$list);
        $this->template->load('lendplats/commentlist');
    }
    //修改评论
    function set_comment(){
        if($_REQUEST){
            $mssage = array();

            $id    = trim($_REQUEST['id']);
            $key  = trim($_REQUEST['key']);
            $value  = trim($_REQUEST['value']);
            if(!$id){
                    $mssage['info']	  = '系统错误';
                    $mssage['status'] = 0;
                    $mssage['url']	  = '';
                    echo json_encode($mssage);exit;
            }
            $data = array();
            $data[$key] = $value;
            $result = $this->db->where(array("id"=>$id))->update("tan_lend_comment",$data);
            if($result){
                $mssage['info']	  = '修改成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url('lendplats/commentlist');
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '修改失败';
                $mssage['status'] = 0;
                $mssage['url']	  = site_url('lendplats/commentlist');
                echo json_encode($mssage);exit;
            }
        }
    }
    
    //导入线下评论
    function impor_lend_comment(){
        if($_POST){
            $my_excel = new Mexcel();
            $file_name = trim($_POST['file_url']);
            $result = $my_excel->excel_to_array($file_name);
            if($result['status'] == 1 ){
                $list_ = $result['data'];
                if(!$list_){
                    $mess['status'] = 1;
                    $mess['error'] = '没有发现任何数据';
                    echo json_encode($mess);die;
                }
                $list = array();
                $flag_name = 0;//判断平台名称是否正确
                $flag_phone = 0;//判断手机号格式是否正确
                $flag_str = "";
                foreach ($list_ as $key=>$val){
                    if(!preg_match("/^1[3|4|5|7|8]\d{9}$/", $val[0])){
                        $flag_str .= $val[0];
                        $flag_phone = 1;
                        break;
                    }

                    $temp = array();
                    $temp['mobile'] = $val[0];
                    $temp['email'] = $val[1];
                    $temp['true_name'] = $val[2];
                    $temp['ID_card'] = $val[3];
                    $list[] = $temp;
                }
                if($flag_phone){
                    $mess['status'] = 1;
                    $mess['error'] = '手机号码格式解析错误,第'.($key+1)."条：".$flag_str;
                    echo json_encode($mess);die;
                }
                
                $import_success = 0;//成功导入
                $import_fail = 0;//失败导入
                $import_success_str = '';//成功导入
                $import_fail_str = '';//失败导入
                $datas = array();
                foreach ($list as $key=>$val){
                    //检测是否存在有线上用户
                    $user = $this->users_model->get_user_by_mobile($val['mobile']);
                    //检测是否存在下线用户
                    $user_ = $this->users_model->get_user_line_by_mobile($val['mobile']);
                    if($user){
                        $import_fail_str.$val['mobile'].",";
                        $import_fail ++;
                    }elseif($user_){
                        $import_fail_str.$val['mobile'].",";
                        $import_fail ++;
                    }else{
                        $temp = array();
                        $temp['mobile'] = $val["mobile"];
                        $temp['email'] = $val["email"];
                        $temp['true_name'] = $val["true_name"];
                        $temp['ID_card'] = $val["ID_card"];
                        $datas[] = $temp;
                        $import_success_str.$val['mobile'].",";
                        $import_success ++;
                        
                    }
                }
                if($datas){
                    $this->users_model->insert_line_users($datas);
                }
                $mess['status'] = 0;
                $mess['info'] = '成功导入'.$import_success.'条数据['.$import_success_str.'],失败导入'.$import_fail.'条数据['.$import_fail_str.']';
            }else{
                $mess['status'] = 1;
                $mess['error'] = $result['error'];
            }
            
            echo json_encode($mess);die;
        }
    }
    
    //平台攻略
    function raiders(){
        
        $where = "1 = 1";
        $getcount = $this->db->query("select * from tan_lend_raiders r left join tan_lend_list l on l.id = r.lend_id where $where order by r.id desc")->num_rows();
        $navigation_position = 'outside';
        $records_per_page = 10;
        $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position, array('left', 'right')) ? $navigation_position : 'outside');
        //$this->xpage->reverse(true);
        $this->xpage->records($getcount);
        $this->xpage->records_per_page($records_per_page);
        $this->xpage->labels("<<", ">>");
        $query_param = "r.*,l.name_ch,l.name_en";
        $list = $this->db->query("select $query_param from tan_lend_raiders r left join tan_lend_list l on l.id = r.lend_id where $where order by r.id desc limit ".(($this->xpage->get_page() - 1) * $records_per_page).",".$records_per_page)->result_array();
        foreach ($list as $key=>$val){
                
        }
        $this->template->assign('Total',$getcount);
        $this->template->assign('showpage',$this->xpage->render(true));
        $this->template->assign('list',$list);
        $this->template->load('lendplats/raiders');
    }
    //添加贷款攻略
    function raiders_add(){
        if($_POST){
            $data = array();
            $data['title'] = trim($_POST['title']);
            $data['sort'] = 0;
            $data['content'] = trim($_POST['content']);
            $data['status'] = trim($_POST['status']);
            $data['lend_id'] = trim($_POST['lend_id']);
            $data['create_time'] = time();
            if(!$data['title']){
                $mssage['info']	  = '标题不能为空';
                $mssage['status'] = 0;
                $mssage['url']	  = "";
                echo json_encode($mssage);exit;
            }
            
            if(!$data['lend_id']){
                $mssage['info']	  = '请选择平台';
                $mssage['status'] = 0;
                $mssage['url']	  = "";
                echo json_encode($mssage);exit;
            }
            if(!$data['content']){
                $mssage['info']	  = '内容不能为空';
                $mssage['status'] = 0;
                $mssage['url']	  = "";
                echo json_encode($mssage);exit;
            }
            
            $result = $this->db->insert("tan_lend_raiders",$data);
            if($result){
                $mssage['info']	  = '添加成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url('lendplats/raiders');
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '添加失败';
                $mssage['status'] = 0;
                $mssage['url']	  = site_url('lendplats/raiders');
                echo json_encode($mssage);exit;
            }
            
        }
        
        $lend_list = $this->db->query("select id,name_ch from tan_lend_list where status = 1 order by id desc")->result_array();
        $this->template->assign('lend_list',$lend_list);
        $this->template->assign('segment_p','raiders');
        $this->template->load('lendplats/raiders_add');
    }
    //修改贷款攻略
    function raiders_edit(){
        if($_POST){
            $id = trim($_POST['id']);
            $data = array();
            $data['title'] = trim($_POST['title']);
            $data['content'] = trim($_POST['content']);
            $data['status'] = trim($_POST['status']);
            $data['lend_id'] = trim($_POST['lend_id']);
            if(!$data['title']){
                $mssage['info']	  = '标题不能为空';
                $mssage['status'] = 0;
                $mssage['url']	  = "";
                echo json_encode($mssage);exit;
            }
            
            if(!$data['lend_id']){
                $mssage['info']	  = '请选择平台';
                $mssage['status'] = 0;
                $mssage['url']	  = "";
                echo json_encode($mssage);exit;
            }
            if(!$data['content']){
                $mssage['info']	  = '内容不能为空';
                $mssage['status'] = 0;
                $mssage['url']	  = "";
                echo json_encode($mssage);exit;
            }
            
            $result = $this->db->where("id",$id)->update("tan_lend_raiders",$data);
            if($result){
                $mssage['info']	  = '修改成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url('lendplats/raiders');
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '修改失败';
                $mssage['status'] = 0;
                $mssage['url']	  = site_url('lendplats/raiders');
                echo json_encode($mssage);exit;
            }
            
        }
        $id = trim($_GET['id']);
        $item  = $this->db->query("select * from tan_lend_raiders where id = $id order by id limit 1")->row_array();
        $lend_list = $this->db->query("select id,name_ch from tan_lend_list where status = 1 order by id desc")->result_array();
        $this->template->assign('lend_list',$lend_list);
        $this->template->assign('segment_p','raiders');
        $this->template->assign('item',$item);
        $this->template->load('lendplats/raiders_edit');
    }
    function raiders_setsort(){
       if($_POST){
            $mssage = array();
            $id    = trim($_POST['id']);
            $sort  = trim($_POST['sort']);
            if(!$id){
                    $mssage['info']	  = '系统错误';
                    $mssage['status'] = 0;
                    $mssage['url']	  = '';
                    echo json_encode($mssage);exit;
            }

            $data = array();
            $data['sort'] = $sort;
            $result = $this->db->where(array("id"=>$id))->update("tan_lend_raiders",$data);
            if($result){
                $mssage['info']	  = '修改成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url('lendplats/raiders');
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '修改失败';
                $mssage['status'] = 0;
                $mssage['url']	  = site_url('lendplats/raiders');
                echo json_encode($mssage);exit;
            }
        }
    }
     function raiders_settitle(){
       if($_POST){
            $mssage = array();
            $id    = trim($_POST['id']);
            $title  = trim($_POST['title']);
            if(!$id){
                    $mssage['info']	  = '系统错误';
                    $mssage['status'] = 0;
                    $mssage['url']	  = '';
                    echo json_encode($mssage);exit;
            }

            $data = array();
            $data['title'] = $title;
            $result = $this->db->where(array("id"=>$id))->update("tan_lend_raiders",$data);
            if($result){
                $mssage['info']	  = '修改成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url('lendplats/raiders');
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '修改失败';
                $mssage['status'] = 0;
                $mssage['url']	  = site_url('lendplats/raiders');
                echo json_encode($mssage);exit;
            }
        }
    }
    
    function raiders_setstatus(){
       if($_REQUEST){
            $mssage = array();
            $id    = trim($_REQUEST['id']);
            $status  = trim($_REQUEST['status']);
            if(!$id){
                    $mssage['info']	  = '系统错误';
                    $mssage['status'] = 0;
                    $mssage['url']	  = '';
                    echo json_encode($mssage);exit;
            }

            $data = array();
            $data['status'] = $status;
            $result = $this->db->where(array("id"=>$id))->update("tan_lend_raiders",$data);
            if($result){
                $mssage['info']	  = '修改成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url('lendplats/raiders');
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '修改失败';
                $mssage['status'] = 0;
                $mssage['url']	  = site_url('lendplats/raiders');
                echo json_encode($mssage);exit;
            }
        }
    }
    private function www_base_url($url){
        return "http://2016.tan66.com/".$url.".html";
    }


    //平台排名规则
    function platrule(){
        $where = array();
        //分页
        $getcount = $this->reload_rule_model->get_count($where);

        $navigation_position = 'outside';
        $records_per_page = 10;
        $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position, array('left', 'right')) ? $navigation_position : 'outside');
        $this->xpage->records($getcount);
        $this->xpage->records_per_page($records_per_page);
        $this->xpage->labels("<<", ">>");
        $rule_tlist = $this->reload_rule_model->get_list($where, (($this->xpage->get_page() - 1) * $records_per_page) ,$records_per_page);

        foreach($rule_tlist as $key=>$vo){

            $rule_tlist[$key]['name'] = $vo['name'];
            $rule_tlist[$key]['type'] = $vo['type'];
            if($vo['type']==1){
                $click = '跳转量';
            }elseif($vo['type']==2){
                $click = '浏览量';
            }
            if($vo['time_type']==0) {
                $time_str= '无限制';
            }elseif($vo['time_type']==1) {
                $time_str= '每天';
            }elseif($vo['time_type']==2){
                $time_str = $vo['time_value'].'分钟';
            }elseif($vo['time_type']==3){
                $time_str = $vo['time_value'];
            }
            $over = $vo['rule_direct']?"超过":"不足";
            $condition_str = $time_str.$click.$over.$vo['num'];
            $rule_tlist[$key]['condition'] = $condition_str;
            $rule_tlist[$key]['add_time'] = date('Y-m-d H:i:s',$vo['add_time']);
            $rule_tlist[$key]['is_valid'] = $vo['is_valid']?"是":"否";
        }
        //重定义页面
        $this->template->assign('Total',$getcount);
        $this->template->assign('showpage',$this->xpage->render(true));
        $this->template->assign('list',$rule_tlist);
        $this->template->load('lendplats/platrule');
    }
    //平台排名规则添加
    function platrule_add(){
        $all_plat = $this->lend_list_model->get_all_list(array('is_mobile'=>1,'status'=>1));
        if($_POST){
            $data = array();
            if(!trim($_POST['rule_name'])){
                $mssage['info']	  = '名称必须输入';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }

            if($this->reload_rule_model->get_rule(array('rule_name'=>trim($_POST['name'])))){
                $mssage['info']	  = '名称已经存在';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
//            if(!trim($_POST['num'])){
//                $mssage['info']	  = '满足数量值必须输入';
//                $mssage['status'] = 0;
//                $mssage['url']	  = '';
//                echo json_encode($mssage);exit;
//            }
            if(!trim($_POST['score'])){
                $mssage['info']	  = '分值必须输入';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
//            if(!trim($_POST['type'])){
//                $mssage['info']	  = '跳转类型必须输入';
//                $mssage['status'] = 0;
//                $mssage['url']	  = '';
//                echo json_encode($mssage);exit;
//            }
//            if(!trim($_POST['time_type'])){
//                $mssage['info']	  = '时间段设置类型必须输入';
//                $mssage['status'] = 0;
//                $mssage['url']	  = '';
//                echo json_encode($mssage);exit;
//            }

            $time_value = trim($_POST['time_value']);
            if(  trim($_POST['time_type'])==2 && empty($time_value)){
                $mssage['info']	  = '时间段类型非全天请输入相关值';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }

            $start_time = trim($_POST['start_time']);
            $end_time = trim($_POST['end_time']);
            if(  trim($_POST['time_type'])==3){
                //比较开始时间和结束时间
                $temp_start_time = strtotime($start_time);
                $temp_end_time = strtotime($end_time);
                if($temp_start_time>=$temp_end_time){
                    $mssage['info']	  = '时间设置错误';
                    $mssage['status'] = 0;
                    $mssage['url']	  = '';
                    echo json_encode($mssage);exit;
                }
            }

            $data['is_valid'] = $_POST['is_valid'];
            $data['valid_hour'] = trim($_POST['valid_hour']);
            $data['rule_name'] = trim($_POST['rule_name']);
            $data['num'] = trim($_POST['num']);
            $data['score'] = trim($_POST['score']);
            $data['type'] = trim($_POST['type']);
            $data['time_type'] = trim($_POST['time_type']);
            $data['time_value'] = trim($_POST['time_value']);
            $data['rule_direct'] = trim($_POST['rule_direct']);
            $data['time_effect'] = trim($_POST['rule_start_time']).'--'.trim($_POST['rule_end_time']);
            $data['add_time'] = time();
            if($data['time_type']==3){
                $data['time_value'] = $start_time.'--'.$end_time;
            }
            $plat_id = $_POST['plat_id'];
            if(empty($plat_id)){
                $data['plat_id'] = 0;
            }else{
                $data['plat_id'] = implode(',',$plat_id);
            }

            $result = $this->reload_rule_model->insert($data);
            if($result){
                $logdata = array();
                $logdata['user_id'] = $this->userinfo['id'];
                $logdata['username'] = $this->userinfo['username'];
                $logdata['event'] = 'system';
                $logdata['content'] = '增加规则：'.$_POST['rule_name'];
                $this->admin_log_model->insert($logdata);


                $mssage['info']	  = '增加成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url('lendplats/platrule');
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '新增不成功';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }

        }

        $this->template->assign('all_plat',$all_plat);
        $this->template->assign('act_t','新增');
        $item = array(
            'valid_hour'=>0,
            'is_valid'=>1,
            'rule_redirect'=>1,
            'rule_start_time'=>'00:00',
            'rule_end_time'=>'24:00',
        );
        $this->template->assign('item',$item);
        $this->template->load('lendplats/platrule_info');
    }
    //平台排名规则编辑
    function platrule_edit(){
        $id = intval($_GET['id']);
        if(!$id){
            redirect('platrule/index');
        }
        if($_POST){
            $id = trim($_POST['id']);
            $data = array();
            if(!trim($_POST['rule_name'])){
                $mssage['info']	  = '名称必须输入';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }

            if($this->reload_rule_model->get_rule(array('rule_name'=>trim($_POST['name'])))){
                $mssage['info']	  = '名称已经存在';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
//            if(!trim($_POST['num'])){
//                $mssage['info']	  = '满足数量值必须输入';
//                $mssage['status'] = 0;
//                $mssage['url']	  = '';
//                echo json_encode($mssage);exit;
//            }
            if(!trim($_POST['score'])){
                $mssage['info']	  = '分值必须输入';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
//            if(!trim($_POST['type'])){
//                $mssage['info']	  = '跳转类型必须输入';
//                $mssage['status'] = 0;
//                $mssage['url']	  = '';
//                echo json_encode($mssage);exit;
//            }
//            if(!trim($_POST['time_type'])){
//                $mssage['info']	  = '时间段设置类型必须输入';
//                $mssage['status'] = 0;
//                $mssage['url']	  = '';
//                echo json_encode($mssage);exit;
//            }

            $time_value = trim($_POST['time_value']);
            if(  trim($_POST['time_type'])==2 && empty($time_value)){
                $mssage['info']	  = '时间段类型非全天请输入相关值';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
            $start_time = trim($_POST['start_time']);
            $end_time = trim($_POST['end_time']);
            if(  trim($_POST['time_type'])==3){
                //比较开始时间和结束时间
                $temp_start_time = strtotime($start_time);
                $temp_end_time = strtotime($end_time);
                if($temp_start_time>=$temp_end_time){
                    $mssage['info']	  = '时间设置错误';
                    $mssage['status'] = 0;
                    $mssage['url']	  = '';
                    echo json_encode($mssage);exit;
                }
            }

            $data['is_valid'] = $_POST['is_valid'];
            $data['valid_hour'] = trim($_POST['valid_hour']);
            $data['rule_name'] = trim($_POST['rule_name']);
            $data['num'] = trim($_POST['num']);
            $data['score'] = trim($_POST['score']);
            $data['type'] = trim($_POST['type']);
            $data['time_type'] = trim($_POST['time_type']);
            $data['time_value'] = trim($_POST['time_value']);
            $data['rule_direct'] = trim($_POST['rule_direct']);
            $data['time_effect'] = trim($_POST['rule_start_time']).'--'.trim($_POST['rule_end_time']);
            if($data['time_type']==3){
                $data['time_value'] = $start_time.'--'.$end_time;
            }
            $plat_id = $_POST['plat_id'];
            if(empty($plat_id)){
                $data['plat_id'] = 0;
            }else{
                $data['plat_id'] = implode(',',$plat_id);
            }
            $result = $this->reload_rule_model->update($data,array('id'=>$id));
            if($result){
                $logdata = array();
                $logdata['user_id'] = $this->userinfo['id'];
                $logdata['username'] = $this->userinfo['username'];
                $logdata['event'] = 'system';
                $logdata['content'] = '修改规则：'.$_POST['name'].'-'.$_POST['value'];

                $this->admin_log_model->insert($logdata);


                $mssage['info']	  = '修改成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url('lendplats/platrule');
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '操作失败';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
        }

        $item = $this->reload_rule_model->get_rule(array('id'=>$id));
        $all_plat = $this->lend_list_model->get_all_list(array('is_mobile'=>1,'status'=>1));
        foreach ($all_plat as $key => $one_plat){
            $plat_id = explode(',',$item['plat_id']);
            if(in_array($one_plat['id'],$plat_id)){
                $all_plat[$key]['is_exists'] = 1;
            }else{
                $all_plat[$key]['is_exists'] = 0;
            }
        }
        //时间区间处理
        $time_value = $item['time_value'];
        if($item['time_type']==3){
            $value_arr = explode('--',$time_value);
            $item['start_time'] = $value_arr[0];
            $item['end_time'] = $value_arr[1];
        }
        $time_effect = explode('--',$item['time_effect']);
        $item['rule_start_time'] = isset($time_effect[0])?$time_effect[0]:"00:00";
        $item['rule_end_time'] = isset($time_effect[1])?$time_effect[1]:"24:00";
        $this->template->assign('act_t','修改');
        $this->template->assign('item',$item);
        $this->template->assign('all_plat',$all_plat);
        $this->template->load('lendplats/platrule_info');
    }
    //平台排名规则删除
    function platrule_del(){
        $id	   = intval($_GET['id']);
        if(!$id){
            redirect('lendplats/platrule');
        }

        $result = $this->reload_rule_model->delete(array('id'=>$id));
        if($result){
            $logdata = array();
            $logdata['user_id'] = $this->userinfo['id'];
            $logdata['username'] = $this->userinfo['username'];
            $logdata['event'] = 'platrule';
            $logdata['content'] = '删除规则：【'.$_GET['name'].'】';
            $this->admin_log_model->insert($logdata);

            $mssage['info']	  = '删除成功';
            $mssage['status'] = 1;
            $mssage['url']	  = $_SERVER['HTTP_REFERER'];
            echo json_encode($mssage);exit;
        }else{
            $mssage['info']	  = '删除失败';
            $mssage['status'] = 0;
            $mssage['url']	  = $_SERVER['HTTP_REFERER'];
            echo json_encode($mssage);exit;
        }
    }
    
}


